import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/login_controller.dart';
import '../l10n/app_localizations.dart';

class LoginDialog extends StatelessWidget {
  const LoginDialog({super.key});

  @override
  Widget build(BuildContext context) {
    final LoginController controller = Get.find();
    final l10n = AppLocalizations.of(context);

    return Obx(() {
      if (!controller.isLoginDialogVisible.value) {
        return const SizedBox.shrink();
      }

      return Stack(
        children: [
          // 背景遮罩
          GestureDetector(
            onTap: controller.hideLoginDialog,
            child: Container(color: Colors.black.withOpacity(0.5)),
          ),

          // 弹窗内容
          Center(
            child: Container(
              width: 320.w,
              margin: EdgeInsets.symmetric(horizontal: 20.w),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    const Color(0xFF0084FF).withOpacity(0.8),
                    const Color(0xFF0084FF),
                  ],
                ),
                borderRadius: BorderRadius.circular(20.r),
                border: Border.all(color: const Color(0xFF00FFFF), width: 2),
                boxShadow: [
                  BoxShadow(
                    color: const Color(0xFF00FFFF).withOpacity(0.3),
                    blurRadius: 20,
                    spreadRadius: 2,
                  ),
                ],
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SizedBox(height: 30.h),

                  // 欢迎标题
                  Text(
                    l10n.welcomeToLogin,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

                  SizedBox(height: 30.h),

                  // 机器人头像
                  // _buildRobotAvatar(),
                  Image.asset(
                    "assets/images/icon_login_logo.png",
                    width: 126.w,
                    fit: BoxFit.fill,
                  ),

                  SizedBox(height: 30.h),

                  // 登录按钮
                  _buildLoginButtons(controller, l10n),

                  SizedBox(height: 30.h),
                ],
              ),
            ),
          ),
        ],
      );
    });
  }

  Widget _buildLoginButtons(LoginController controller, AppLocalizations l10n) {
    return Column(
      children: [
        // 苹果登录按钮
        _buildLoginButton(
          icon: Icons.apple,
          text: l10n.useApple,
          onTap: controller.loginWithApple,
          isApple: true,
        ),

        SizedBox(height: 16.h),

        // 谷歌登录按钮
        _buildLoginButton(
          icon: Icons.g_mobiledata,
          text: l10n.google,
          onTap: controller.loginWithGoogle,
          isApple: false,
        ),
      ],
    );
  }

  Widget _buildLoginButton({
    required IconData icon,
    required String text,
    required VoidCallback onTap,
    required bool isApple,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 280.w,
        height: 50.h,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1),
              blurRadius: 8,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 图标
            SizedBox(
              width: 24.w,
              height: 24.w,
              child:
                  isApple
                      ? Icon(Icons.apple, color: Colors.black, size: 24.sp)
                      : _buildGoogleIcon(),
            ),

            SizedBox(width: 12.w),

            // 文字
            Text(
              text,
              style: TextStyle(
                color: Colors.black,
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildGoogleIcon() {
    return Container(
      width: 24.w,
      height: 24.w,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: LinearGradient(
          colors: [
            const Color(0xFF4285F4), // 蓝色
            const Color(0xFF34A853), // 绿色
            const Color(0xFFFBBC05), // 黄色
            const Color(0xFFEA4335), // 红色
          ],
          stops: const [0.0, 0.3, 0.6, 1.0],
        ),
      ),
      child: Center(
        child: Text(
          'G',
          style: TextStyle(
            color: Colors.white,
            fontSize: 14.sp,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}
